package com.controller;

import com.alibaba.fastjson.JSON;
import com.alibaba.fastjson.JSONObject;
import com.pojo.TabEmp;
import com.service.DeptService;
import com.service.EmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@Controller
// 基于Restful风格:get 查询 \ post 添加 \ put 修改 \ delete 删除
@RequestMapping("/emp")
public class EmpController {
    final EmpService empService;
    final DeptService deptService;

    @Autowired
    public EmpController(EmpService empService, DeptService deptService) {
        this.empService = empService;
        this.deptService = deptService;
    }

    //    Restful风格: GET 查询
    @RequestMapping(method = RequestMethod.GET)
    public String empAll(Model model) {
        model.addAttribute("list", empService.empAll());
        model.addAttribute("dept",deptService.deptAll());
        return "EmpPage";
    }

    //    修改
    @RequestMapping("/empUpdate")
    public String empUpdate(Model model) {
       model.addAttribute("dept",deptService.deptAll());
        return "/EmpPage";
    }

    //    修改
    @RequestMapping (value = "/getEmp/{id}" , produces = "application/json;charset=utf-8")
    @ResponseBody
    public String getEmp(@PathVariable("id") Integer empId) {
        System.out.println(empId);
        return JSON.toJSONString(empService.empById(empId));
    }

    //    删除
    @DeleteMapping("/{id}")
//    @RequestMapping(value = "/{id}",method = RequestMethod.DELETE)
    public String empDelete(@PathVariable("id") Integer empId) {
        Integer num = empService.empDeleteById(empId);
        return "redirect:/emp";
    }

    //    添加
    @PostMapping
    public String empInsert() {
        return "";
    }

}
